﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;
using System.IO;

public partial class SupportVideoEN : System.Web.UI.Page
{

    

    string strChangedFiles = "";


    public List<SupportVideos> lstSupportVideosGrid
    {
        get
        {
            if (ViewState["lstSupportVideosGrid"] == null)
            {
                ViewState["lstSupportVideosGrid"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstSupportVideosGrid"].ToString()] == null)
            {
                Session[ViewState["lstSupportVideosGrid"].ToString()] = new List<SupportVideos>();

            }

            return (List<SupportVideos>)Session[ViewState["lstSupportVideosGrid"].ToString()];

        }
        set
        {
            if (ViewState["lstSupportVideosGrid"] == null)
            {
                ViewState["lstSupportVideosGrid"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstSupportVideosGrid"].ToString()] = value;
        }   
   
   

    }




    long mSupportVideosID
    {
        get
        {
            if (ViewState["mSupportVideosID"] == null)
            {
                ViewState["mSupportVideosID"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["mSupportVideosID"].ToString()] == null)
            {
                long obj = -1;
                Session[ViewState["mSupportVideosID"].ToString()] = obj;

            }

            return (long)Session[ViewState["mSupportVideosID"].ToString()];

        }
        set
        {
            if (ViewState["mSupportVideosID"] == null)
            {
                ViewState["mSupportVideosID"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["mSupportVideosID"].ToString()] = value;
        }
    }


    SupportVideos mObjSupportVideos
    {
        get
        {
            if (ViewState["mObjSupportVideos"] == null)
            {
                ViewState["mObjSupportVideos"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["mObjSupportVideos"].ToString()] == null)
            {
                Session[ViewState["mObjSupportVideos"].ToString()] = new SupportVideos();

            }

            return (SupportVideos)Session[ViewState["mObjSupportVideos"].ToString()];

        }
        set
        {
            if (ViewState["mObjSupportVideos"] == null)
            {
                ViewState["mObjSupportVideos"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["mObjSupportVideos"].ToString()] = value;
        }
    }



    Persons mobjUser
    {
        get
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            return Session[ViewState["mobjUser"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();

            Session[ViewState["mobjUser"].ToString()] =  value;
        }
    }






    protected void Page_Load(object sender, EventArgs e)
    {



       
       
        if (!Page.IsPostBack)
        {
            //mobjUser = GlobalVars.getCurrentPerson();
            checkSecurityAndCustomizePage(mobjUser);
            bindGrid("");
           
        }

        

    }



    /// <summary>
    /// this function will bind the grid 
    /// </summary>
    protected void bindGrid(string strIn)
    {

        List<SupportVideos> lstSupportVideos = search(strIn);



        lstSupportVideosGrid = lstSupportVideos;

        grdUsers.DataSource = lstSupportVideos;
        grdUsers.DataBind();
    }



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check

        //if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Support_Videos_Page))
        //    Response.Redirect("LoginEN.aspx");


        
       




    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        bindGrid(txtSearch.Text);
       
    }

    /// <summary>
    /// this function will search in hints and return the values
    /// </summary>
    /// <param name="txtText">the text to search for</param>
    /// <returns>a list of hints matched</returns>
    protected List<SupportVideos> search(string txtText)
    {
        SupportVideosDB objSupportVideosDB = new SupportVideosDB(ConnectionStrings.LMS);
        List<SupportVideos> lstRet = new List<SupportVideos>();
        if (txtText.Trim() == "")
        {
            List<SupportVideos> lstNew = objSupportVideosDB.Select(" top 100 ", " order by [VideoTitleEN] ");
            //lstNew = new List<TicketType>(from objFol in lstNew where objFol.StatusID != 2 select objFol);
            //no status
            return lstNew;
        }

        string[] strValues = null;
        if (txtText.Trim()[0] == '\"' && txtText.Trim()[txtText.Trim().Length - 1] == '\"')
        {
            try
            {
                strValues = new string[] { txtText.Trim().Substring(1, txtText.Trim().Length - 2) };
                if (strValues[0] == "")
                    strValues = new string[0];
            }
            catch
            {
                strValues = new string[0];
            }
        }
        else
        {
            strValues = txtText.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }

        foreach (string strValue in strValues)
        {
            SupportVideos objSupportVideos = new SupportVideos();
            objSupportVideos.VideoTitleAR = objSupportVideos.VideoTitleEN = objSupportVideos.VideoDescriptionAR = objSupportVideos.VideoDescriptionEN = "%" + strValue + "%";
            List<SupportVideos> lstSupportVideos = objSupportVideosDB.SearchOrLike(objSupportVideos);
            //lstTicketType = new List<TicketType>(from objFol in lstTicketType where objFol.StatusID != 2 select objFol);
            //no status
            foreach (SupportVideos objFol in lstSupportVideos)
            {
                if ((from objIn in lstRet where objIn.ID == objFol.ID select objIn).ToList().Count == 0)
                {
                    lstRet.Add(objFol);
                }
            }

        }


        return lstRet;



    }





  




   


    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.DataSource = lstSupportVideosGrid;
        grdUsers.PageIndex = e.NewPageIndex;

        grdUsers.DataBind(); 
    }




    



    protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SupportVideosDB objSupportVideosDB = new SupportVideosDB(ConnectionStrings.LMS);


            long SupportVideosID = 0;
            long.TryParse(grdUsers.DataKeys[e.Row.RowIndex]["ID"].ToString(),out SupportVideosID);

            List<SupportVideos> lstSupportVideos = objSupportVideosDB.getSupportVideosByID(SupportVideosID);

            if (lstSupportVideos.Count == 0)
                return;

            try
            {
                FileInfo fi = new FileInfo(Server.MapPath("./SupportVideos/" + lstSupportVideos[0].VideoFile));

                Label lblSize = new Label();

                Panel pnlFileSize = (Panel)e.Row.FindControl("pnlFileSize");

                lblSize.Text = (((float)fi.Length)/(1024 * 1024)) .ToString("f3");
                pnlFileSize.Controls.Add(lblSize);
            }
            catch
            {
            }

        }
    }
}
